package cn.attackme.myuploader.utils;

import com.spire.pdf.PdfDocument;
import com.spire.pdf.PdfPageBase;

import java.awt.*;
import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

//非税收发票测试
public class Test2 {
    public static void main(String[] args) throws IOException {
        //创建PdfDocument实例
        PdfDocument doc = new PdfDocument();
        //加载PDF文件
        doc.loadFromFile("C:\\Users\\admin\\Desktop\\invoice\\11010121_0028289510.PDF");
        PdfPageBase pdfPageBase = doc.getPages().get(0);
        String contents = pdfPageBase.extractText().replaceAll(" ","").replaceAll("　","").replaceAll("：",":");
        System.out.println(contents);
        //发票代码
        String p_code = "票据代码:\\d{8}";
        Pattern r_code = Pattern.compile(p_code);
        Matcher m_code = r_code.matcher(contents);
        if(m_code.find()) {
            String code = m_code.group().replaceAll("票据代码:","");
            System.out.println("发票代码："+code);
        }
        //识别发票号码
        //String numContent = pdfPageBase.extractText(new Rectangle(413, 34, 117, 18)).replaceAll(" ","").replaceAll("　","");
        String p_num = "(票据号码:\\d{10})";
        Pattern r_num = Pattern.compile(p_num);
        Matcher m_num = r_num.matcher(contents);
        if(m_num.find()) {
            String num = m_num.group().replaceAll("票据号码:","");
            System.out.println("发票号码："+num);
        }

        //发票日期
        //String timeContent = pdfPageBase.extractText(new Rectangle(413, 47, 129, 20)).replaceAll(" ","").replaceAll("　","").replaceAll("年","").replaceAll("月","").replaceAll("日","");
        String p_time = "(开票日期:\\d{4}年\\d{2}月\\d{2}日)|(开票日期:[0-9]{8})|(开票日期:\\d{4}\\-\\d{2}\\-\\d{2})|(开票日期:\\d{4}/\\d{2}/\\d{2})";
        Pattern r_time = Pattern.compile(p_time);
        Matcher m_time = r_time.matcher(contents);
        if(m_time.find()) {
            String time = m_time.group().replaceAll("开票日期:","").replaceAll("年","").replaceAll("月","")
                    .replaceAll("日","").replaceAll("-","").replaceAll("/","");
            time = time.substring(0, 4) + "/" + time.substring(4, 6) + "/" + time.substring(6);
            System.out.println("开票日期："+time);
        }

        //校验码后6位
        //String checkContent = pdfPageBase.extractText(new Rectangle(413, 64, 170, 18)).replaceAll(" ","").replaceAll("　","");
        String p_check = "(校验码:\\S{6})";
        Pattern r_check = Pattern.compile(p_check);
        Matcher m_check = r_check.matcher(contents);
        if(m_check.find()) {
            String check = m_check.group().replaceAll("校验码:","");
            System.out.println("校验码："+check);
        }
        //开票方名称

        for (String line:contents.split("\\n")) {
            if(line.contains("收款单位")){
                line = line.substring(line.indexOf(":")+1,line.indexOf(":",line.indexOf(":")+1)).replaceAll("复核人","").replaceAll("收款人","");
                System.out.println("开票方："+line);
                break;
            }
        }


        //金额
        String p_price = "(([(]|[（])(小写)([)]|[）])(\\d+\\.\\d+))|(([(]|[（])(小写)([)]|[）])([¥]|[￥])(\\d+\\.\\d+))";
        Pattern r_price = Pattern.compile(p_price);
        Matcher m_price = r_price.matcher(contents);
        String price = "0";
        if(m_price.find()) {
            price = m_price.group().replaceAll("小写","").replaceAll("\\(","").replaceAll("（","")
                    .replaceAll("）","").replaceAll("\\)","").replaceAll("¥","").replaceAll("￥","");
            System.out.println("金额："+price);
        }
        //税额
       String tax = "0";
       System.out.println("税额："+tax);


        //总价
        String sum = price;
        System.out.println("总价："+sum);

    }
}
